<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Session;

/**
* 
*/
class Order extends AdminBase
{

	public $statusName = [
        "0" => "已提交",
        "1" => "已确认",
        "2" => "已支付",
        "5" => "确认支付"
    ];
	
	public function index(){
		if( request()->isAjax() ){
            $map = input("param.");
            if( empty($map["status"]) ){
                $where = " status >= 0 ";
            }else{
                $where = " status = ".$map["status"];
            }

            if( !empty($map["start_time"]) ){

                $where .= " AND buy_time >= ".strtotime($map["start_time"]);
            }

            if( !empty($map["end_time"]) ){
                if( empty($map["start_time"]) ){
                    $time = time() - (60*60*24*7);
                    $where .= " AND buy_time >= ".$time;
                }
                $where .= " AND buy_time <= ".strtotime($map["end_time"]);
            }

            if( !empty($map["order_code"]) ){

                $where = " order_code = '".$map["order_code"]."'";
            }

            
            $count = Db::name("Order")->where($where)->count();
            

            $limitForm = ( $map["page"] -1 )* $map["limit"];
            $list = Db::name("Order")
                ->where($where)
                ->order("id desc")
                ->limit($limitForm,$map["limit"])
                ->select();
            $data = [];

            foreach($list as $k=>$v){
                $data[$k]["id"] = $v["id"];
                $data[$k]["order_sn"] = $v["order_sn"];
                $data[$k]["user_id"] = $v["user_id"];
                $data[$k]["goods_name"] = $v["goods_name"];
                $data[$k]["goods_price"] = $v["goods_price"];
                $data[$k]["num"] = $v["num"];
                $data[$k]["amount"] = $v["amount"];
                $data[$k]["add_time"] = date('y-m-d H:i:s', $v["add_time"]);
                $data[$k]["statusName"] = $this->statusName[$v["status"]];
                $data[$k]["statusVal"] = $v["status"];
            }

            $retArr = [
                "code"=>0,
                "msg" => "",
                "count" => $count,
                "data" => $data
            ];
            echo json_encode($retArr);
            exit();
        }else{

            $map = input('param.');
            $url = '/admin/order/index';
            $Arr = [];
            if( empty($map["status"]) ){
                $url.="?status=0";
                $Arr["status"] = '0';
            }else{
                $url.="?status=".$map["status"];
                $Arr["status"] = $map["status"];
            }


            if( !empty($map["start_time"]) ){
                $url.="&start_time=".$map["start_time"];
                $Arr["start_time"] = $map["start_time"];
            }
            if( !empty($map["end_time"]) ){
                $url.="&end_time=".$map["end_time"];
                $Arr["end_time"] = $map["end_time"];
            }
            if( !empty($map["order_sn"]) ){
                $url.="&order_sn=".$map["order_sn"];
                $Arr["order_sn"] = $map["order_sn"];
            }
            $this->assign("Arr",$Arr);
            $this->assign("url",$url);
        }

		return $this->fetch();
	}

	//订单详情
    public function details(){

        $id = input('param.id');
        if( empty($id) ){
            $this->error("缺少ID");
        }

        $Arr = Db::name("Order")->alias("a")
            ->join("ysy_user b","a.user_id = b.id","left")
            ->field("a.*,b.nickname")
            ->where("a.id",$id)
            ->find();
        $Arr["statusName"] = $this->statusName[$Arr["status"]];

        $this->assign("Arr",$Arr);
        return $this->fetch();
    }

    //线下付款确认
    public function pay(){
        if( request()->isAjax() ){
            $map = input("post.");
            if( empty($map["id"]) ){
                $retArr = [
                    "id" => 1,
                    "msg" => "操作有误"
                ];
                $this->json_return($retArr);
            }
            $orderArr = Db::name("Order")->alias("a")
                ->join("Goods g","a.goods_id = g.id","left")
                ->field("a.*,g.type")
                ->where("a.id",$map["id"])->find();
            if($orderArr['type'] == 1){//购买的商品
                $user_type = get_user_hy_level($orderArr['user_id']);
                if($orderArr['goods_price'] == 378)
                    $type = 2;
                if($user_type <=1){
                    user_up($orderArr['user_id'],$type,$orderArr['amount']);
                }else{
                    gouwufanli($orderArr['user_id'],$orderArr['amount']);
                }
            }elseif($orderArr['type'] == 2){//购买到会员
                user_up($orderArr['user_id'],3,$orderArr['amount']);
            }elseif($orderArr['type'] == 3){//购买的代理
                $type = select_agent_type($orderArr['goods_price']);
                user_up_agent($orderArr['user_id'],$type,$orderArr['amount']);
            }
            $pdid = Db::name("Order")->where("id",$map["id"])->update(array("status"=>2));
            if( $pdid ){
                //Db::name("userinfo")->where("user_id",$orderArr["user_id"])->update($Arr);
                $retArr = [
                    "id" => 0,
                    "msg" => "操作成功"
                ];
            }else{
                $retArr = [
                    "id" => 5,
                    "msg" => "操作失败"
                ];
            }
            $this->json_return($retArr);
        }
    }

}